package com.it2297.view;

import java.awt.BorderLayout;

import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.it2297.controller.ProductController;
import com.it2297.model.Comment;
import com.it2297.model.Product;

public class UpdatedProductDialog extends JDialog {
	private final ProductController pc = new ProductController(this);
	private final Product product = new Product();
	private final JLabel lblprodName = new JLabel("Product Name:");
	private final JLabel lblprodDesc = new JLabel("Description:");
	private final JLabel lblprodPrice = new JLabel("Price:");
	private final JLabel lblprodQuantity = new JLabel("Quantity:");
	private final JTextField ProductNameTxt = new JTextField();
	private final JTextArea ProductDescTxt = new JTextArea();
	private final JTextField ProductPriceTxt = new JTextField();
	private final JTextField ProductQuantityTxt = new JTextField();
	private final JButton btnSubmit = new JButton("Update");
	private final JLabel lblType = new JLabel("Product Type:");
	private final JButton btnNewButton = new JButton("Choose an Image");
	private final JLabel lbl_img = new JLabel("");
	private ImageIcon img = null;
	private final JTextField typeTxt = new JTextField();



	/**
	 * Create the panel.
	 */
	public UpdatedProductDialog() {
		typeTxt.setEditable(false);
		typeTxt.setBounds(121, 204, 86, 20);
		typeTxt.setColumns(10);
		this.setBounds(100, 100, 600, 450);
		this.setLocationRelativeTo(null);
		ProductQuantityTxt.setBounds(121, 173, 86, 20);
		ProductQuantityTxt.setColumns(10);
		ProductPriceTxt.setBounds(121, 148, 86, 20);
		ProductPriceTxt.setColumns(10);
		ProductNameTxt.setEditable(false);
		ProductNameTxt.setBounds(121, 8, 158, 20);
		ProductNameTxt.setColumns(10);
		
		
		


		initGUI();
	}

	private void initGUI() {
		getContentPane().setLayout(null);
		lblprodName.setBounds(10, 11, 101, 14);

		getContentPane().add(lblprodName);
		lblprodDesc.setBounds(10, 44, 82, 14);

		getContentPane().add(lblprodDesc);
		lblprodPrice.setBounds(10, 151, 67, 14);

		getContentPane().add(lblprodPrice);
		lblprodQuantity.setBounds(10, 176, 82, 14);

		getContentPane().add(lblprodQuantity);

		getContentPane().add(ProductNameTxt);
		ProductDescTxt.setBounds(121, 39, 269, 104);

		getContentPane().add(ProductDescTxt);

		getContentPane().add(ProductPriceTxt);

		getContentPane().add(ProductQuantityTxt);

		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(ProductPriceTxt.getText().isEmpty() || ProductNameTxt.getText().isEmpty() || ProductDescTxt.getText().isEmpty() || ProductQuantityTxt.getText().isEmpty()){
					JOptionPane.showMessageDialog(null, "Please fill in all field");
				}else{
					boolean successful = updatedProduct();
					if(successful){
					close();
					JOptionPane.showMessageDialog(null, "Update Successful!");
					}else{
						JOptionPane.showMessageDialog(null, "Update UnSuccessful!");
					}
				}
			}
		});
		btnSubmit.setBounds(118, 266, 89, 23);

		getContentPane().add(btnSubmit);
		lblType.setBounds(10, 201, 101, 14);

		getContentPane().add(lblType);

		btnNewButton.setBounds(213, 266, 158, 23);

		getContentPane().add(btnNewButton);
		lbl_img.setBounds(213, 151, 158, 104);

		getContentPane().add(lbl_img);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				img = pc.showImageChooser();
				lbl_img.setIcon(img);
			}
		});

		
		getContentPane().add(typeTxt);
	}
	
	public void setProductName(String name){
		ProductNameTxt.setText(name);
	}
	public void setProductDesc(String desc){
		ProductDescTxt.setText(desc);
	}
	public void setProductPrice(double price){
		ProductPriceTxt.setText(Double.toString(price));
	}
	public void setProductQuantity(int Quantity){
		ProductQuantityTxt.setText(Integer.toString(Quantity));
	}
	public void setProductType(String type){
		typeTxt.setText(type);
	}
	public void setCategory(String category){
		//comboBox.setSelectedIndex(category);
	}
	public void setImg(String name){
		Image bg = Toolkit
				.getDefaultToolkit()
				.createImage(
						System.getProperty("user.dir") + "/img/product-img/"
								+ name + ".png")
				.getScaledInstance(158, 120, Image.SCALE_DEFAULT);
		lbl_img.setIcon(new ImageIcon(bg));
	}
	public String getProductName() {
		return ProductNameTxt.getText();
	}

	public String getProductDesc() {
		return ProductDescTxt.getText();
	}
	public String getProductType(){
		return typeTxt.getText();
	}

	public double getProductPrice() {
		return Double.parseDouble(ProductPriceTxt.getText());
	}

	public int getProductQuantity() {
		return Integer.parseInt(ProductQuantityTxt.getText());
	}
	
	

	private boolean updatedProduct() {
		pc.saveImagesUpdated();
		return pc.UpdatedProduct();
	}

	public void close() {
		this.dispose();
	}
}
